*---------------------------------------------------------------------
version 11
cap clear
cap log close
set more off
cap set mem 700m

cd "E:\REStat_MS14767_Vol96(2)\Data preparation Compustat segment"
log using "6_raw_rjv_panel_america.log", replace
*-----------------------------------------------------------------

****************************************
* This short file  drops non-american firms
* from the RJV data set as well as observations from 1985
* and entries without ticker.
****************************************


*************
* generate inside/outside dummy
*************

use "raw_rjv_panel.dta", clear
gen ins=0
replace ins=1 if year>= eyear
replace ins=0 if year> xyear

*************
*we drop all infos from 1985 (we do not have COMPUSTAT data for that year)
*************

count if year==1985
* 14016
drop if year==1985


*************
*we drop all firms that have no ticker (we cannot use them in the market share analysis)
*************

count if ticker==""
* 112560

drop if ticker==""


merge m:1 ticker year using "ticker_year.dta"
tab _merge
drop if _merge==2
drop _merge



*-*-*-*-*-*-* NOTE - DATA CORRECTION!!! *-*-*-*-*-*-*-*-*-*

******* 
* here we have to correct for the fact that for each entityname (connected to the TICKER)
* there might be several entrynames.
* we choose to say that the "mother" firm is in the RJV if at least one of the entitynames is in
* we then keep only one observation per entityname, RJV, year.
*************

egen ins2=max(ins), by(comnum rjvnum year)
drop ins
rename ins2 ins
sort year comnum rjvnum
count if rjvnum==rjvnum[_n-1] & year==year[_n-1] & comnum==comnum[_n-1]
* 8652
drop if rjvnum==rjvnum[_n-1] & year==year[_n-1] & comnum==comnum[_n-1]

*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*

save raw_rjv_panel_segment.dta, replace

log close
